package com.android.porttallymansystem.ui.fragment

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.FragmentSelfBinding
import com.android.porttallymansystem.logic.Setting
import com.android.porttallymansystem.ui.activity.SettingsActivity
import com.android.porttallymansystem.ui.activity.UserInfoActivity

class SelfFragment : Fragment() {

    private lateinit var binding: FragmentSelfBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentSelfBinding.inflate(inflater, container, false)

        initObserve()

        setListener()

        return binding.root
    }

    private fun initObserve() {
        Setting.user.observe(viewLifecycleOwner, Observer {
            binding.tvSelfUserName.text = Setting.user.value!!.name
            binding.tvSelfUserId.text = String.format(resources.getString(R.string.user_id), Setting.user.value!!.id)
            binding.ivUserProfile.setImageResource(Setting.user.value!!.profile)
        })
    }

    private fun setListener() {
        binding.btUserInfo.setOnClickListener {
            val intent = Intent(context, UserInfoActivity::class.java)
            startActivity(intent)
        }

        binding.btSettings.setOnClickListener {
            val intent = Intent(context, SettingsActivity::class.java)
            startActivity(intent)
        }
    }
}